package com.ouc.ouc.interceptor;

import com.google.common.collect.Maps;
//import com.ouc.openplatform.pampas.InnerCookie;
//import com.ouc.openplatform.pampas.UserUtil;
import io.terminus.pampas.common.InnerCookie;
import io.terminus.pampas.common.UserUtil;
import io.terminus.pampas.webc.utils.CookieBuilder;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class CookieInterceptor extends HandlerInterceptorAdapter {
	
	
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		Map<String, String> cookieValues = Maps.newHashMap();
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				cookieValues.put(cookie.getName(), cookie.getValue());
			}
		}
		InnerCookie innerCookie = new InnerCookie(cookieValues);
		UserUtil.putInnerCookie(innerCookie);
		return true;
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		InnerCookie innerCookie = UserUtil.getInnerCookie();
		if (innerCookie == null) {
			return;
		}
		for (InnerCookie.FakeCookie fakeCookie : innerCookie.getNewCookies()) {
			CookieBuilder cookieBuilder = CookieBuilder
					.from(fakeCookie.getName(), fakeCookie.getValue(), fakeCookie.getDomain())
					.path(fakeCookie.getPath()).maxAge(fakeCookie.getAge());
			if (fakeCookie.isHttpOnly()) {
				cookieBuilder.httpOnly();
			}
			response.addCookie(cookieBuilder.build());
		}
		for (InnerCookie.FakeCookie fakeCookie : innerCookie.getDelCookies()) {
			CookieBuilder cookieBuilder = CookieBuilder.from(fakeCookie.getName(), null, fakeCookie.getDomain())
					.path(fakeCookie.getPath()).maxAge(0);

			response.addCookie(cookieBuilder.build());
		}
		UserUtil.clearInnerCookie();
	}
}
